package org.pfc.porlapatilla.repositories;

import java.util.List;

import org.pfc.porlapatilla.model.Message;
import org.pfc.porlapatilla.model.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

/**
 * This Repository provides data access to MessageService
 * 
 * @author jorgepolobermudez
 * 
 */
public interface MessageRepository extends
	PagingAndSortingRepository<Message, String> {

    /**
     * Find a message by id.
     * 
     * @param id
     *            The message id.
     * @return The found message.
     */
    Message findById(String id);

    /**
     * Find a message list by sender.
     * 
     * @param sender
     *            The message sender.
     * @return The list of found messages.
     */
    List<Message> findBySender(User sender);

    /**
     * Find a message list by sender id.
     * 
     * @param senderId
     *            The message sender id.
     * @return The list of found messages.
     */
    @Query("{ 'sender': { '$ref': 'user', '$id': { '$oid': ?0 } } }")
    List<Message> findBySenderId(String senderId, Sort sort);

    /**
     * Find a message page by sender id.
     * 
     * @param sender
     *            The message sender id.
     * @param pageable
     *            The page access data.
     * @return The page of found messages.
     */
    @Query("{ 'sender': { '$ref': 'user', '$id': { '$oid': ?0 } } }")
    Page<Message> findBySenderId(String sender, Pageable pageable);

    /**
     * Find a message list by receiver id.
     * 
     * @param receiverId
     *            The receiver id.
     * @return The list of found messages.
     */
    @Query("{ 'receiver': { '$ref': 'user', '$id': { '$oid': ?0 } } }")
    List<Message> findByReceiverId(String receiverId, Sort sort);

    /**
     * Find a message page by receiver id.
     * 
     * @param receiverId
     *            The receiver id.
     * @param pageable
     *            The page access data.
     * @return The page of found messages.
     */
    @Query("{ 'receiver': { '$ref': 'user', '$id': { '$oid': ?0 } } }")
    Page<Message> findByReceiverId(String receiverId, Pageable pageable);
    
}
